﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LeoK.Common;
using LeoK.TM.Entity;
using MySql.Data.MySqlClient;
using Telerik.Windows.Controls;

namespace LeoK.TM
{
    /// <summary>
    /// TaskManager.xaml 的交互逻辑
    /// </summary>
    public partial class TaskManager : UserControl
    {
        private List<TM_Sln> _Slns; 
        private LeoKDataGrid<TM_Task> _leoKDataGrid;
        private GridViewComboBoxColumn _slnCol;

        public TaskManager()
        {
            InitializeComponent();

            _leoKDataGrid = new LeoKDataGrid<TM_Task>();
            layout.Children.Add(_leoKDataGrid);
            
            _leoKDataGrid.BeforeGeneratingColumn = BeforeGeneratingColumn;
            _leoKDataGrid.BeforeSaveData = BeforeSaveData;
            Loaded += async (sender, args) =>
            {
                await LoadSlnData();
                DataSpanSelector.SelectionChanged();
            };

            DataSpanSelector.SelectionChanged = () =>
            {
                string filter = " WHERE StartTime>=@StartDate AND StartTime<=@EndDate";
                List<MySqlParameter> paras = new List<MySqlParameter>();
                paras.Add(new MySqlParameter("StartDate", DataSpanSelector.StartDate));
                paras.Add(new MySqlParameter("EndDate", DataSpanSelector.EndDate));

                _leoKDataGrid.LoadData(filter, paras);
            };
        }

        private void BeforeSaveData(TM_Task task)
        {
            if (task.IsFinish)
            {
                task.EndTime = DateTime.Now;
            }
        }

        private bool BeforeGeneratingColumn(GridViewAutoGeneratingColumnEventArgs e)
        {
            bool result = false;

            if (e.Column.UniqueName == "SlnID")
            {
                _slnCol = new GridViewComboBoxColumn();
                _slnCol.UniqueName = "SlnID";
                _slnCol.Header = "所属项目";

                _slnCol.IsComboBoxEditable = false;
                _slnCol.DisplayMemberPath = "Name";
                _slnCol.SelectedValueMemberPath = "ID";
                _slnCol.ItemsSource = _Slns;

                e.Column = _slnCol;
                result = true;
            }

            return result;
        }

        public async Task LoadSlnData()
        {
            using (MySqlDB db = new MySqlDB())
            {
                _Slns = await db.QueryObjs<TM_Sln>("SELECT * FROM TM_Sln");
                if (_slnCol != null)
                {
                    _slnCol.ItemsSource = _Slns;
                }
                _leoKDataGrid.LoadData();
            }
        }

        private void btnRefresh_Click(object sender, MouseButtonEventArgs e)
        {
            DataSpanSelector.SelectionChanged();
        }

        private async void btnCalc_Click(object sender, MouseButtonEventArgs e)
        {
            using (MySqlDB db = new MySqlDB())
            {
                string updateSql = @"UPDATE TM_Task A INNER JOIN
                                    (
	                                    SELECT TID, SUM(TimeSpan) AS Total FROM TM_TimeLine GROUP BY TID  
                                    ) AS C ON A.ID=C.TID
                                    SET A.TimeSpend=C.Total";
                await db.ExecuteNonQuery(updateSql, null);
            }

            DataSpanSelector.SelectionChanged();
        }
    }
}
